package top.glike.ssl.auto.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import top.glike.ssl.auto.domain.other.CertInfo;
import top.glike.ssl.common.annotation.Excel;
import top.glike.ssl.common.core.domain.BaseEntity;

import java.util.Date;

/**
 * 域名管理对象 ssl_domain_certificate
 *
 * @author Sophia
 * @date 2025-10-12
 */
@Data
public class SslDomainCertificate extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /**
     * 主键ID
     */
    private Long id;

    /**
     * 主域名
     */
    @Excel(name = "主域名")
    private String mainDomain;

    /**
     * 域名
     */
    @Excel(name = "域名")
    private String domainName;

    /**
     * 证书名称
     */
    @Excel(name = "证书名称")
    private String certificateName;

    /**
     * 证书ID
     */
    @Excel(name = "证书ID")
    private String certificateId;

    /**
     * 订单ID
     */
    @Excel(name = "订单ID")
    private String orderId;

    /**
     * 证书规格
     */
    @Excel(name = "证书规格")
    private String productCode;

    /**
     * 用户名称
     */
    @Excel(name = "用户名称")
    private String username;

    /**
     * 用户电话
     */
    @Excel(name = "用户电话")
    private String phone;

    /**
     * 用户邮箱
     */
    @Excel(name = "用户邮箱")
    private String email;

    /**
     * 验证方式
     */
    @Excel(name = "验证方式")
    private String validateType;

    /**
     * 云厂商ID
     */
    @Excel(name = "云厂商ID")
    private Long cloudProviderId;

    /**
     * 服务器ID
     */
    @Excel(name = "服务器ID")
    private Long serverId;

    /**
     * 证书文件名
     */
    @Excel(name = "证书文件名")
    private String certFileName;

    /**
     * 私钥文件名
     */
    private String keyFileName;

    /**
     * 证书本地存储路径
     */
    @Excel(name = "证书本地存储路径")
    private String certLocalPath;

    /**
     * 私钥本地存储路径
     */
    private String keyLocalPath;

    /**
     * 证书在服务器的完整路径
     */
    @Excel(name = "证书在服务器的完整路径")
    private String certServerPath;

    /**
     * 私钥在服务器的完整路径
     */
    private String keyServerPath;

    /**
     * 是否自动续期
     */
    @Excel(name = "是否自动续期")
    private Integer autoRenew;

    /**
     * 续期后是否删去老的证书数据
     */
    @Excel(name = "续期后是否删去老的证书数据")
    private Integer deleteOldCert;

    /**
     * 是否已删除记录
     */
    @Excel(name = "是否已删除记录")
    private Boolean isDelete;

    /**
     * 到期前多少天续期
     */
    @Excel(name = "到期前多少天续期")
    private Long renewBeforeDays;

    /**
     * 状态
     */
    @Excel(name = "状态")
    private String status;

    /**
     * 证书颁发时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "证书颁发时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date issueTime;

    /**
     * 证书过期时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "证书过期时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date expireTime;

    /**
     * 最后续期时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "最后续期时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date lastRenewTime;

    /**
     * 最后同步到服务器时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "最后同步到服务器时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date lastSyncTime;

    /**
     * 描述
     */
    @Excel(name = "描述")
    private String description;

    /**
     * 证书Pem信息
     */
    @Excel(name = "证书Pem信息")
    private String certificatePem;

    /**
     * 证书key信息
     */
    @Excel(name = "证书key信息")
    private String privateKey;

    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "创建时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date createdTime;

    /**
     * 更新时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "更新时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date updatedTime;

    public SslDomainCertificate() {
    }

    public SslDomainCertificate(CertInfo cert, Long cloudProviderId) {
        this.certificateId = String.valueOf(cert.getCertId());
        this.domainName = cert.getDomain();
        this.status = cert.getStatus();
        this.issueTime = cert.getCertStartTime();
        this.expireTime = cert.getCertEndTime();
        this.isDelete = false;
        this.validateType = "DNS";
        this.renewBeforeDays = 2l;
        this.createdTime = new Date();
        this.updatedTime = this.createdTime;
        this.cloudProviderId = cloudProviderId;
        this.deleteOldCert = 1;
        this.description = "拉取云数据同步到数据库";
        this.certFileName = domainName.replace(".", "_");
    }

    public void setNewCertInfo(CertInfo cert) {
        this.certificateId = String.valueOf(cert.getCertId());
        this.status = cert.getStatus();
        this.issueTime = cert.getCertStartTime();
        this.expireTime = cert.getCertEndTime();
        this.updatedTime = new Date();
        this.description = "拉取云数据同步到数据库";
    }


    /**
     * 检查证书是否即将到期
     */
    public boolean checkNearExpiry() {
        if (this.expireTime == null) {
            return false;
        }

        long currentTime = System.currentTimeMillis();
        long expireTime = this.expireTime.getTime();
        long daysDiff = (expireTime - currentTime) / (24 * 60 * 60 * 1000);

        Boolean nearExpiry = (daysDiff <= this.getRenewBeforeDays() && daysDiff >= 0);

        return nearExpiry;
    }

    /**
     * 检查证书是否已过期
     */
    public boolean isExpired() {
        if (this.expireTime == null) {
            return false;
        }
        boolean expired = new Date().after(this.expireTime);

        return expired;
    }
}
